package com.anycc.cloud.core.entity;

import com.anycc.cloud.common.entity.BaseEntity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 区划实体类
 */
@Entity
@Table(
        name = "sys_area",
        indexes = {
                @Index(columnList = "name", name = "i_sys_area_name"),
                @Index(columnList = "code", name = "i_sys_area_code"),
                @Index(columnList = "version", name = "i_sys_area_version")
        }
)
public class Area extends BaseEntity {
    private static final long serialVersionUID = 2371382793334937966L;
    public static final String FIELD_NAME = "name";
    public static final String FIELD_CODE = "code";
    public static final String FIELD_VERSION = "version";
    public static final String FIELD_LEVEL = "levelNum";
    public static final String FIELD_PARENT = "parent";
    public static final String FIELD_CHILDREN = "children";

    private String code;
    private String name;
    private String version;
    private Integer levelNum;
    private Area parent;
    private List<Area> children = new ArrayList<>();

    public Area() {
    }

    @Column(
            name = "code",
            length = 36,
            unique = true
    )
    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    @Column(
            name = "name",
            length = 64,
            nullable = false
    )
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(
            name = "version",
            length = 16
    )
    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    @Column(
            name = "level_num"
    )
    public Integer getLevelNum() {
        return levelNum;
    }

    public void setLevelNum(Integer levelNum) {
        this.levelNum = levelNum;
    }

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(
            name = "parent_id"
    )
    @JsonIgnoreProperties({"children", "parent", "updateTime", "createTime"})
    public Area getParent() {
        return parent;
    }

    public void setParent(Area parent) {
        this.parent = parent;
    }

    @OneToMany(
            mappedBy = "parent", fetch = FetchType.EAGER
    )
    @JsonIgnore
    @OrderBy("code,name")
    public List<Area> getChildren() {
        return children;
    }

    public void setChildren(List<Area> children) {
        this.children = children;
    }
}
